package com.atguigu.test.juctest.cas;

import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * 使用csa之后
 */
class MyStudent{


   AtomicInteger atomicInteger =new AtomicInteger();

    public AtomicInteger getAtomicInteger() {
        return atomicInteger;
    }

    public void setAtomicInteger() {
       atomicInteger.getAndIncrement();
    }
}
public class CasAfter {
    public static void main(String[] args) {

        //线程操作资源类
        MyStudent myStudent = new MyStudent();
        for (int i = 1; i <= 200; i++) {
            new Thread(() -> {
                for (int t = 1; t <= 200; t++) {
                    myStudent.setAtomicInteger();
                }
            }, String.valueOf(i)).start();


        }
          try { TimeUnit.SECONDS.sleep(3); } catch (InterruptedException e) { e.printStackTrace(); }
          System.out.println(Thread.currentThread().getName()+"\t"+"---come in"+myStudent.getAtomicInteger());
    }
}
